package BlogMybatis.blog.service.impl;

import BlogMybatis.blog.pojo.Comment;
import BlogMybatis.blog.mapper.CommentMapper;
import BlogMybatis.blog.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Brandt
 * @since 2020-06-07
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    //存放迭代找出所有子代回复的集合
    private List<Comment> tempReplys = new ArrayList<>();

    @Override
    public List<Comment> listCommentByBlogId(Long blogId) {
        //查找所有的父级点
        List<Comment> comments = commentMapper.findByBlogIdParentIdNull(blogId, Long.parseLong("-1"));
        for (Comment comment : comments) {
            //父级评论的id
            Long id = comment.getId();
            //父级评论的昵称
            String parentNickname1  = comment.getNickname();
            //查找父级点的所有子回复
            List<Comment> childComments  = commentMapper.findByBlogIdParentIdNotNull(blogId, id);
            combineChildren(blogId,childComments,parentNickname1);
            comment.setReplyComments(tempReplys);
            tempReplys = new ArrayList<>();

        }
        return comments;
    }

    private void combineChildren(Long blogId, List<Comment> childComments, String parentNickname1) {
        //判断是否有一级子评论
        if(childComments.size() > 0){
            //循环找出子评论的id
            for(Comment childComment : childComments){
                String parentNickname = childComment.getNickname();
                childComment.setParentNickname(parentNickname1);
                tempReplys.add(childComment);
                Long childId = childComment.getId();
                //查询出子二级评论
                recursively(blogId, childId, parentNickname);
            }
        }
    }

    private void recursively(Long blogId, Long childId, String parentNickname1) {
        //根据子一级评论的id找到子二级评论
        List<Comment> replayComments = commentMapper.findByBlogIdAndReplayId(blogId,childId);

        if(replayComments.size() > 0){
            for(Comment replayComment : replayComments){
                String parentNickname = replayComment.getNickname();
                replayComment.setParentNickname(parentNickname1);
                Long replayId = replayComment.getId();
                tempReplys.add(replayComment);
                recursively(blogId,replayId,parentNickname);
            }
        }
    }

    @Override
    public int saveComments(Comment comment) {
        //获取要评论内容的id，如果不为空，说明是一个子评论
        if(comment.getParentComment().getId() != null){
            //设置子回复的父级id
            comment.setParentCommentId(comment.getParentComment().getId());
        }
        comment.setCreateTime(new Date());
        return commentMapper.saveComments(comment);
    }
}
